using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    public class DistributorController : Controller
    {
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();
        //
        // GET: /Distributor/

        public ActionResult Index()
        {
            return View(_db.NHA_PHAN_PHOI.ToList());
        }

        //
        // GET: /Distributor/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Distributor/Create

        public ActionResult Create()
        {

            return View();
        } 

        //
        // POST: /Distributor/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude="id")]NHA_PHAN_PHOI nppToCreate)
        {
            try
            {
                _db.AddToNHA_PHAN_PHOI(nppToCreate);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Distributor/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View(_db.NHA_PHAN_PHOI.Where(npp=>npp.id ==id).First());
        }

        //
        // POST: /Distributor/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(NHA_PHAN_PHOI nppToEdit)
        {
            try
            {
                // TODO: Add update logic here
                NHA_PHAN_PHOI originalNPP = _db.NHA_PHAN_PHOI.Where(npp => npp.id == nppToEdit.id).First();
                _db.ApplyPropertyChanges(originalNPP.EntityKey.EntitySetName, nppToEdit);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
